package clientView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import clientMain.Login;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;

public class EmCom_List extends JPanel implements ActionListener{

	private int pageCur;
	private JPanel panelHeader;
	private JPanel panelBody;
	public static JTable tblEmComList;
	public static JScrollPane scrollPane;	
	private JPanel panelPage;
	private JButton btnNext;
	public static JComboBox cbbPage;
	private JButton btnAdd;
	private JButton btnBack;
	private JCheckBox chckbxFullEm;
	private boolean check = true;
	
	
	public EmCom_List(int page){
		pageCur = page;
		setLayout(new BorderLayout(0, 0));
		panelHeader = UtilityGUI.headerForm("Danh Sách Nhân Viên Trong Công Ty");
		add(panelHeader, BorderLayout.NORTH);
		
		panelBody = new JPanel();
		GridBagLayout gbl_panelBody = new GridBagLayout();
		gbl_panelBody.columnWidths = new int[]{0, 0};
		gbl_panelBody.rowHeights = new int[]{0, 0, 0};
		gbl_panelBody.columnWeights = new double[]{1.0, 0.0};
		gbl_panelBody.rowWeights = new double[]{0.0, 1.0, 0.0};
		panelBody.setLayout(gbl_panelBody);
		add(panelBody, BorderLayout.CENTER);
		
		Listener listener = new Listener();		
		chckbxFullEm = new JCheckBox("Toàn bộ NV(bao gồm cả nghỉ việc)");
		chckbxFullEm.setFont(UtilityGUI.fontLblForm);
		chckbxFullEm.addItemListener(listener);
		GridBagConstraints gbc_chckbxFullEm = new GridBagConstraints();
		gbc_chckbxFullEm.anchor = GridBagConstraints.WEST;
		gbc_chckbxFullEm.insets = new Insets(10, 10, 5, 5);
		gbc_chckbxFullEm.gridx = 0;
		gbc_chckbxFullEm.gridy = 0;
		panelBody.add(chckbxFullEm, gbc_chckbxFullEm);		
		
		for(int i=0; i<Login.listAuthorization.size();i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("Employee") && 
					Login.listAuthorization.get(i).getPermName().equals("Add")){
				btnAdd = new JButton("Thêm Nhân Viên");
				btnAdd.setFont(UtilityGUI.fontBtnForm);
				btnAdd.addActionListener(this);
				GridBagConstraints gbc_btnAdd = new GridBagConstraints();
				gbc_btnAdd.fill = GridBagConstraints.BOTH;
				gbc_btnAdd.insets = new Insets(10, 0, 5, 10);
				gbc_btnAdd.gridx = 1;
				gbc_btnAdd.gridy = 0;
				panelBody.add(btnAdd, gbc_btnAdd);
				break;
			}
		}
		
		tblEmComList = UtilityGUI.createTblListEmCom(true, pageCur, "Em_Id", true);		
		tblEmComList.addMouseListener(new Mouse_tblEmCom());
		
		scrollPane = new JScrollPane();		
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 10, 5, 10);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 2;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		scrollPane.setViewportView(tblEmComList);
		panelBody.add(scrollPane, gbc_scrollPane);
		
		panelPage = new JPanel();
		GridBagConstraints gbc_panelPage = new GridBagConstraints();
		gbc_panelPage.gridwidth = 2;
		gbc_panelPage.insets = new Insets(0, 10, 10, 10);
		gbc_panelPage.fill = GridBagConstraints.BOTH;
		gbc_panelPage.gridx = 0;
		gbc_panelPage.gridy = 2;
		panelBody.add(panelPage, gbc_panelPage);
		panelPage.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnBack = new JButton(UtilityGUI.iconBack);
		btnBack.addActionListener(this);
		panelPage.add(btnBack);
		
		cbbPage = UtilityGUI.cbbPaging("vi_EMCompany");
		cbbPage.addActionListener(this);
		panelPage.add(cbbPage);
		
		btnNext = new JButton(UtilityGUI.iconNext);
		btnNext.addActionListener(this);
		panelPage.add(btnNext);
	}
	
	private class Listener implements ItemListener {		
		public void itemStateChanged(ItemEvent itemEvent) {
	        if(itemEvent.getSource() == chckbxFullEm){
	        	tblEmComList.setVisible(false);
	        	int state = itemEvent.getStateChange();
		        if (state == ItemEvent.SELECTED) {
		        	check = false;
		        	tblEmComList = UtilityGUI.createTblListEmCom(check, pageCur, "Em_Id", true);
		        }else if (state == ItemEvent.DESELECTED) {
		        	check = true;
		        	tblEmComList = UtilityGUI.createTblListEmCom(check, pageCur, "Em_Id", true);
		        }
		        tblEmComList.addMouseListener(new Mouse_tblEmCom());
		        scrollPane.setViewportView(tblEmComList);
	        }
		}
	}
	
	public static class Mouse_tblEmCom extends MouseAdapter{
		public void mouseClicked(MouseEvent e) {
			int row = tblEmComList.getSelectedRow();
			String emId  = (String) tblEmComList.getModel().getValueAt(row, 0);
			if(e.isControlDown() && e.getButton() == e.BUTTON1){
				int index = Main.jTabbedPane.positionTab(UtilityGUI.tabEmInfo);
				if(index == -1){
					Main.jTabbedPane.addTab(new Employee_View(emId), UtilityGUI.tabEmInfo);
					Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				}else{
					Main.jTabbedPane.removeTabAt(index);
					Main.jTabbedPane.addTab(new Employee_View(emId), UtilityGUI.tabEmInfo, index);
					Main.jTabbedPane.setSelectedIndex(index);
					return;
				}
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnAdd){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabEmAdd);
			if(index == -1){
				Main.jTabbedPane.addTab(new Employee_Add(), UtilityGUI.tabEmAdd);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
			 	Main.jTabbedPane.setSelectedIndex(index);
			 	return;
			}
		}else if(e.getSource() == btnBack){
			if(pageCur == 1){
				return;
			}
			tblEmComList.setVisible(false);
			pageCur = pageCur - 1;
			cbbPage.setSelectedItem(pageCur);
			tblEmComList = UtilityGUI.createTblListEmCom(check, pageCur, "Em_Id", true);
			tblEmComList.addMouseListener(new Mouse_tblEmCom());
			scrollPane.setViewportView(tblEmComList);
		}else if(e.getSource() == btnNext){
			if(pageCur == cbbPage.getItemCount()){
				return;
			}
			tblEmComList.setVisible(false);
			pageCur = pageCur + 1;
			cbbPage.setSelectedItem(pageCur);
			tblEmComList = UtilityGUI.createTblListEmCom(check, pageCur, "Em_Id", true);
			tblEmComList.addMouseListener(new Mouse_tblEmCom());
			scrollPane.setViewportView(tblEmComList);
		}else if(e.getSource() == cbbPage){
			int pageGo = (Integer) cbbPage.getSelectedItem();
			if(pageCur == pageGo){
				return;
			}else{
				pageCur = pageGo;
				tblEmComList.setVisible(false);
				tblEmComList = UtilityGUI.createTblListEmCom(check, pageCur, "Em_Id", true);
				tblEmComList.addMouseListener(new Mouse_tblEmCom());
				scrollPane.setViewportView(tblEmComList);
			}
		}
	}
	
}
